﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using ComLib.Caching;
using ComLib.Entities;
using ComLib.Extensions;
using ComLib.Data;
using ComLib.Web.Modules.Pages;
using ComLib.Web.Modules.MenuEntrys;
using ComLib.Web.Lib.Controllers;


namespace ComLib.CMS.Controllers
{
    /// <summary>
    /// Controller for Dynamic Html content pages.
    /// </summary>
    [HandleError]
    public class MenuEntryController : EntityControllerAdmin<MenuEntry>
    {
        /// <summary>
        /// OVerride the url to use after creation.
        /// </summary>
        protected override void Init()
        {
            Conf["CreateSuccessUrl"] = "index";
            Conf["UpdateSuccessUrl"] = "index";
        }
    }



    /// <summary>
    /// Controller for Dynamic Html content pages.
    /// </summary>
    [HandleError]
    public class PageController : EntityControllerAdmin<Page>
    {
        /// <summary>
        /// Get the page from the database/cache and show it in the view.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Show(int id)
        {
            // These can be cached.
            Page page = Page.Lookup[id];
            if (page == null || !page.IsPublic) return View("Pages/NotFound");

            return InternalDetails(page);
        }


        /// <summary>
        /// Show the page based by using the page name.
        /// </summary>
        /// <param name="pagename"></param>
        /// <returns></returns>
        public ActionResult ShowByName(string pagename)
        {
            Page page = MenuEntry.GetPage(pagename);
            if (page == null || !page.IsPublic) return View("Pages/NotFound");

            return InternalDetails(page);
        }

        
        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles="Admin")]
        public override ActionResult Create(Page entity)
        {               
            string addToMenu = this.Request.Params["addToMenu"];
            ActionResult result = base.Create(entity);
            bool isPartOfMenu = string.Compare(addToMenu, "on", true) == 0;

            if(isPartOfMenu)  
                MenuEntry.CreateFromPage(entity);

            return result;
        }


        [Authorize(Roles = "Admin")]
        public override ActionResult Edit(int id, FormCollection collection)
        {
            ActionResult result = base.Edit(id, collection);
            string addToMenu = this.Request.Params["addToMenu"];
            bool isPartOfMenu = string.Compare(addToMenu, "on", true) == 0;
            MenuEntry.UpdateFromPage(isPartOfMenu, id);
            return result;
        }

        
        [Authorize(Roles = "Admin")]
        public override ActionResult Delete(int id, string confirmButton)
        {
            ActionResult result =  base.Delete(id, confirmButton);
            var items = MenuEntry.Find(Query<MenuEntry>.New().Where(m => m.RefId).Is(id));
            if (items != null && items.Count > 0)
            {
                var item = items[0];
                MenuEntry.Delete(item);
            }
            return result;
        }
    }
}
